package com.magicsoft.share.ui.article;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gyf.barlibrary.ImmersionBar;
import com.magicsoft.share.R;
import com.magicsoft.share.bean.article.ArticleContent;
import com.magicsoft.share.bean.article.MyArticleDetail;
import com.magicsoft.share.constant.APIService;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.rx_retrofit_network.location.retrofit.RetrofitUtil;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.LoadingTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.NetWorkTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.SimpleObserver;
import com.magicsoft.share.utils.OssTool;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.utils.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;

/**
 * 文章发布
 */
public class AddNewArticleActivity extends AppCompatActivity {
    @BindView(R.id.tv_title)
    TextView tvTitleCenter;
    @BindView(R.id.tv_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_article_title)
    TextView tvArticleTitle;
    @BindView(R.id.iv_cover)
    ImageView ivCover;
    @BindView(R.id.iv_article_one_cover)
    ImageView ivArticleOneCover;
    private String coverStr;
    private Long articleId;
    private String coverRiginalStr;
    private OssTool ossManager;
    private ArrayList<ArticleContent> articleContents;
    protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_article_two);
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        initView();
    }

    private void initView() {
        tvTitleCenter.setText("编辑");
        tvTitleRight.setText("下一步");

        articleContents = ((ArrayList<ArticleContent>) getIntent().getSerializableExtra("articleContents"));
        long articleId = getIntent().getLongExtra("articleId", -1);
        Log.i("SLL","articleId:"+articleId);
        if (articleId == -1) {
            this.articleId = null;
            coverStr = articleContents.get(0).getImg();
            coverRiginalStr = articleContents.get(0).getImg();
            Glide.with(this).load(coverStr).into(ivCover);
            Glide.with(this).load(coverRiginalStr).into(ivArticleOneCover);

        } else {
            this.articleId = articleId;
            myArticleDetail(articleId);
        }
        ossManager = OssTool.getInstance().init(this);

    }

    @OnClick({R.id.tv_article_title, R.id.iv_cover,R.id.iv_article_close,R.id.iv_article_one_cover,R.id.tv_article_one_content,R.id.tv_right
            ,R.id.ibtn_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //编辑文章标题
            case R.id.tv_article_title:
                    Intent intent = new Intent(AddNewArticleActivity.this, EdtTitleActivity.class);
                    intent.putExtra("titleStr", tvArticleTitle.getText().toString());
                    startActivityForResult(intent, Constant.REQUEST_ARTICLE_TITLE);
                break;
            //编辑文章封面
            case R.id.iv_cover:
                    Intent intent2 = new Intent(AddNewArticleActivity.this, CoverActivity.class);
                    intent2.putExtra("coverStr", coverStr);
                    startActivityForResult(intent2, Constant.REQUEST_ARTICLE_COVER);
                break;
            //删除第一段文章
            case R.id.iv_article_close:
                   ToastUtils.showToast(this,"至少保留一张照片");
                break;
            //编辑第一段图片
            case R.id.iv_article_one_cover:

                break;
            //编辑第一段文字
            case R.id.tv_article_one_content:

                break;
            //下一步
            case R.id.tv_right:

                break;
            //关闭页面
            case R.id.ibtn_left:

                break;
        }
    }


    public void myArticleDetail(long id){
        RetrofitUtil.getApi(APIService.class)
                .myArticleDetail(SPUtils.getInstance().getString(SPKey.TOKEN),id)
                .compose(new NetWorkTransformer<MyArticleDetail>())
                .compose(new LoadingTransformer<MyArticleDetail>(this))
                .subscribe(new SimpleObserver<MyArticleDetail>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull MyArticleDetail myArticleDetail) {
                        ToastUtils.showToast(AddNewArticleActivity.this,"成功了?");
                        articleId = myArticleDetail.getArticle_id();
                        coverStr = myArticleDetail.getCover();
                        Glide.with(AddNewArticleActivity.this).load(coverStr).into(ivCover);
                        coverRiginalStr = myArticleDetail.getCover_riginal();
                        tvArticleTitle.setText(myArticleDetail.getTitle());
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                //获取标题
                case Constant.REQUEST_ARTICLE_TITLE:
                        String articleStr = data.getStringExtra("titleStr");
                        tvArticleTitle.setText(articleStr);
                    break;

                default:

                    break;
            }

        }
    }
}
